//
//  RSWebView+WKUIDelegate.m
//  StaffTax
//
//  Created by 侯伟 on 2020/3/13.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSWebView+WKUIDelegate.h"

@implementation RSWebView (WKUIDelegate)

- (WKWebView*)webView:(WKWebView*)webView createWithConfig:(WKWebViewConfiguration*)config navigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures{
    
    WKFrameInfo *frameInfo = navigationAction.targetFrame;
    if (![frameInfo isMainFrame]) {
        if (navigationAction.request) {
            [webView loadRequest:navigationAction.request];
        }
    }
    
    if(self.UIDelegate && [self.UIDelegate respondsToSelector:@selector(webView:createWithConfig:navigationAction:windowFeatures:)]){
        return [self.UIDelegate webView:webView createWebViewWithConfiguration:config forNavigationAction:navigationAction windowFeatures:windowFeatures];
    }
    return NULL;
}


- (void)webViewDidClose:(WKWebView*)webView{
    
    if(self.UIDelegate && [self.UIDelegate respondsToSelector:@selector(webViewDidClose:)]){
        [self.UIDelegate webViewDidClose:webView];
    }
}

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
    
    if(self.UIDelegate && [self.UIDelegate respondsToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:)]){
        [self.UIDelegate webView:webView runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame completionHandler:completionHandler];
    }else{
        [Alert systemAlert:NULL message:message completion:completionHandler];
    }
}

- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
    
    if(self.UIDelegate && [self.UIDelegate respondsToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:)]){
        [self.UIDelegate webView:webView runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame completionHandler:completionHandler];
    }else{
        [Alert confirm:NULL message:message completion:completionHandler];
    }
}


- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
    
    if(self.UIDelegate && [self.UIDelegate respondsToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:)]){
        [self.UIDelegate webView:webView runJavaScriptTextInputPanelWithPrompt:prompt defaultText:defaultText initiatedByFrame:frame completionHandler:completionHandler];
    }else{
        [Alert prompt:prompt text:defaultText placeholder:NULL completion:completionHandler];
    }
}


@end
 
